package web;

import java.util.function.Consumer;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebHistory.Entry;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

// 网页视图
public class WebViewDemo extends Application {
	private VBox root = new VBox(20);
	private HBox hBox = new HBox(20);
	private WebView wv = new WebView();
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		// 按钮面板
		hBox.setStyle("-fx-background-color: lightgreen");
		hBox.setPrefHeight(100);
		
		Button forward = new Button("前进");
		Button back = new Button("后退");
		Button info = new Button("信息");
		hBox.getChildren().addAll(forward, back, info);
		
		// 网页视图
		WebEngine we = wv.getEngine();
//		wv.setFontScale(1);		// 字体缩放
//		wv.setZoom(1);		// 页面缩放
		we.load("https://www.hao123.com");
		WebHistory wh = we.getHistory();
		root.getChildren().addAll(hBox, wv);
		
		Scene scene = new Scene(root, 700, 600);
		stage.setScene(scene);
		stage.show();
		
		// 事件
		forward.setOnMouseClicked(e -> wh.go(1) );		// 应加上异常处理
		back.setOnMouseClicked(e -> wh.go(-1) );
		info.setOnMouseClicked(e -> {
			System.out.println(wh.getCurrentIndex());
			wh.getEntries().forEach(new Consumer<Entry>() {

				@Override
				public void accept(Entry t) {
					System.out.println(t.getTitle());
					System.out.println(t.getUrl());
				}
			});
		});
	}
}
